package com.renke.jutils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;

public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;	
	public static final int OK = 0;
	public static final int ERROR= 1;
	public R() {
		put("code", OK);
		put("msg", "操作成功");
	}
	
	public static R error() {
		return error(ERROR, "操作失败");
	}

	public static R error(String msg) {
		return error(ERROR, msg);
	}

	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		U.logger.info(msg);
		return r;
	}

	public static R ok(String msg) {
		return R.ok(OK, msg);
	}
	public static R ok(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R ok(Map<String, Object> map) {
		R r = new R();
		r.putAll(map);
		return r;
	}

	public static R ok() {
		return new R();
	}
/**
 * 在成功（ok）方法中增加List类型参数
 * @param T
 * @return
 */
	public static <E> R ok(List<E> es) {
		R r = new R();
		r.put("data",es);
		return r;
	}
	public static <E> R ok(E e) {
		R r = new R();
		r.put("data",e);
		return r;
	}
	public static <E> R ok(Set<E> e) {
		R r = new R();
		r.put("data",e);
		return r;
	}
//	public static <E> R result(List<E> es,int code,String msg) {
//		R r = result(es);
//		if(r.getCode().equals(OK)) {
//			r.put("code", code);
//			r.put("msg", msg);
//			U.logger.info(msg);
//		}
//		return r;
//	}
	public static <E> R result(List<E> es) {
		return R.result(es,"操作失败");
	}
	public static <E> R result(E e) {
		if(e != null)
		{
			List<E> list = new ArrayList<E>();
			list.add(e);
			return R.result(list);
		}
		
		return R.error();
	}
	public static <E> R resultOne(E e) {
		if(e != null)
		{
			return R.result(e);
		}
		
		return R.error();
	}
	public static <E> R result(List<E> es, String msg) {
		if(es != null && !es.isEmpty())
		{
			return R.ok(es);
		}
		return R.error(msg);
	}
	public static <E> R result(Set<E> es, String msg) {
		if(es != null && !es.isEmpty())
		{
			return R.ok(es);
		}
		return R.error(msg);
	}
//	public static <T> R result(int returnValue) {
//		if(returnValue>0)
//		{
//			return R.ok();
//		}
//		return R.error();
//	}
	//Layui中的data必须为列表类型
//	public static <T> R ok(T T) {
//		R r = new R();
//		r.put("data",T);
//		return r;
//	}
	
	public static <E> R ok(List<E> e, Long count)
	{
		return ok(e).put("count", count);
	}
	
	public static R ok(List<Object> list, Integer count)
	{
		return ok(list).put("count", count);
	}
	
	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}
	@SuppressWarnings("unchecked")
	public R add(String key, Object value) {
		if(this.get("data")==null) {
			this.put("data", new HashMap<String,Object>());
		}
		((Map<String,Object>)this.get("data")).put(key, value);
		//super.put(key, value);
		return this;
	}
	@SuppressWarnings("unchecked")
	public Object getObj(String key) {
		if(this.get("data")==null)return null;
		return ((Map<String,Object>)this.get("data")).get(key);
	}
	public static R exception(int errorCode, Exception e) {
		U.errorPrint(e);
		return R.error(errorCode, e.getMessage());
	}
	public R setCode(Integer code) {
		return this.put("code",code);
	}
	public Integer getCode() {
		return (Integer)this.get("code");
	}
	
	public R setMsg(String msg) {
		return this.put("msg",msg);
	}
	public String getMsg() {
		return (String)this.get("msg");
	}
	public R setData(Object data) {
		return this.put("data",data);
	}
	public Object getData() {
		return this.get("data");
	}
	
	public <E> List<E> getDataList() {
		if(getData() instanceof List<?>) {
			return ((List<E>) getData());
		}
		return Arrays.asList((E)getData());
	}
	public <E> E getDataOne() {
		if(getData() instanceof List<?>) {
			return ((List<E>) getData()).get(0);
		}
		return (E) getData();
	}
//	/**
//	 * 判断是否失败,默认succeedCode为 0
//	 * @param succeedCode
//	 * @return
//	 */
//	public boolean isFailed(int succeedCode) {
//		if(!this.getCode().equals(succeedCode)) {
//			return true;
//		}
//		return false;
//	}
	/**
	 * 判断是否失败,默认succeedCode为 0
	 * @return
	 */
//	public boolean isFailed() {
//		return isFailed(0);
//	}
	/**
	 * 判断是否成功,默认succeedCode为 0
	 * @param succeedCode
	 * @return
	 */
	public boolean isSucceed(int succeedCode) {
		if(this.getCode().equals(succeedCode)) {
			return true;
		}
		return false;
	}
	/**
	 * 判断是否成功,默认succeedCode为 0
	 * @return
	 */
	public boolean isSucceed() {
		return isSucceed(0);
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
	public static boolean isError(R r) {
		if(r!= null && !r.getCode().equals(R.OK)) {
			return true;
		}
		return false;
	}
	public static boolean isOk(R r) {
		if(r!= null && r.getCode().equals(R.OK)) {
			return true;
		}
		return false;
	}
}
